package com.darwin.darwinDisplay
{
	import com.darwin.darwinBitmapEngine.core.MovieArrayNode;
	
	import flash.geom.Point;
	
	/**
	 * darx动画绘制完成时抛出此事件 
	 */
	[Event(name="darx_draw_complete",type="com.darwin.darwinDisplay.DarXBitmapMovieNodeEvent")]
	/**
	 *	darx动画播放完成事件  
	 */	
	[Event(name="darx_play_complete",type="com.darwin.darwinDisplay.DarXBitmapMovieNodeEvent")]

	/**
	 *	用于播放单个DarXBitmapData的动画播放类 
	 * @author Roland
	 * 
	 */
	public class DarXBitmapMovieNode extends MovieArrayNode
	{
		private var _darXBitmapData:DarXBitmapData;
		private var _isComplete:Boolean;

		/**
		 *	实例化一个DarXBitmapData动画播放类 
		 * @param darXBitmapData 
		 * 
		 */
		public function DarXBitmapMovieNode(darXBitmapData:DarXBitmapData = null)
		{
			if(darXBitmapData)
			{
				this.darXBitmapData = darXBitmapData;
			}
		}
		
		
		/**
		 *	此播放类要播放的darXBitmapData数据
		 */
		public function get darXBitmapData():DarXBitmapData
		{
			return _darXBitmapData;
		}
		
		/**
		 * @private
		 */
		public function set darXBitmapData(value:DarXBitmapData):void
		{
			//解除对上一份资源的引用
			if(_darXBitmapData)
			{
				DarXBitmapManager.getInstance().reduceSingleQuote(_darXBitmapData);
			}
			_darXBitmapData = value;
			//绘制资源
			if(_darXBitmapData)
			{
				DarXBitmapManager.getInstance().drawDarXBitmapData(_darXBitmapData,onDarXDrawComplete);
				
			}else{
				this.stop();
				this.bitmapData = null;
			}
		}
		
		private var _offsetPoint:Point;
		
		override public function get offsetPoint():Point
		{
			return _offsetPoint;
		}
		
		public function set offsetPoint(value:Point):void
		{
			_offsetPoint = value;
		}
		
		
		protected function onDarXDrawComplete(frames:Array,offsets:Array = null):void
		{
			this.bitmapData = frames[0];
			initMovieNode(frames,offsets);
			_isComplete = true;
			dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE));
		}
		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type, listener, useCapture, priority, useWeakReference);
			if(type == DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE)
			{
				if(_isComplete)
				{
					dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_DRAW_COMPLETE));
				}
			}
		}
		
		
		override public function exeRenderNotify():void
		{
			if(!isStop)
			{
				super.exeRenderNotify();
				dispatchEvent(new DarXBitmapMovieNodeEvent(DarXBitmapMovieNodeEvent.DARX_PLAY_COMPLETE));
			}
		}
		
		override public function render():void
		{
			var $lastFrame:int = currentFrame;
			super.render();
			if($lastFrame != currentFrame && currentFrame==totalFrames-1)
			{
				if(totalFrames>1)
				{
					renderNotify = true;//标记渲染完成通知
				}
			}
		}
		
		override public function destroy():void
		{
			//解除引用
			DarXBitmapManager.getInstance().reduceSingleQuote(_darXBitmapData);
			this.stop();
			super.destroy();
		}

		/**
		 *	动画是否已经绘制完成 
		 */
		public function get isComplete():Boolean
		{
			return _isComplete;
		}

		
	}
}